#include "stu_info.h"

int InputInfo(StuInfo& info){
    std::cout << "continue enter the infomation ?\n";
    char q = getchar();
    if(q == 'q' || q == 'Q'){
        End();
        return 0;
    }
    std::cout << "Name : ";
    std::cin >> info.name;
    std::cout << "\nID : ";
    //CheckNum();
    std::cin >> info.ID;
    std::cout << "\nmath : ";
    //CheckNum();
    std::cin >> info.math;
    std::cout << "\nenglisher : ";
    //CheckNum();
    std::cin >> info.english;
    std::cout << "\ncplusplus : ";
    //CheckNum();
    std::cin >> info.cplusplus;
    std:: cout << "\n";
    getchar();
    info.total = info.math + info.english + info.cplusplus;
    // system("cls");
    return 1;
}

void ShowAll(const StuInfo& info){
    std::cout << "Name : ";
    std::cout << info.name;
    std::cout << "\nID : ";
    std::cout << info.ID;
    std::cout << "\nmath : ";
    std::cout << info.math;
    std::cout << "\nenglisher : ";
    std::cout << info.english;
    std::cout << "\ncplusplus : ";
    std::cout << info.cplusplus;
    std:: cout << "\ntotal ponts is : ";
    std::cout << info.total << std::endl;

    return;
}

void ShowInfo(const StuInfo & info, Subject & sub) {
    switch(sub){
        case Subject::ID_:
            std::cout << "the student " << info.name << "\'s ";
            std::cout << "id is " << info.ID << std::endl;
            break;
        case Subject::MATH:
            std::cout << "the student " << info.name << "\'s ";
            std::cout  << "math point is " << info.math << std::endl;
            break;
        case Subject::ENGLISH:
            std::cout << "the student " << info.name << "\'s ";
            std::cout  << "english point is " << info.english << std::endl;
            break;
        case Subject::CPLUSPLUS:
            std::cout << "the student " << info.name << "\'s ";
            std::cout  << "cplusplus point is " << info.cplusplus << std::endl;
            break;
        case Subject::TOTAL:
            std::cout << "the student " << info.name << "\'s ";
            std::cout  << "total point is " << info.total << std::endl;
            break;
        default:
            std:: cout << "the student " << info.name << "\'s infomation is like below." << std::endl;
            ShowAll(info);
            break;
    }
}

bool SaveInfo(const char * filename, const StuInfo info[], int length=LENGTH_){
    std::ofstream out_file(filename, std::ios::out | std::ios::app);
    if(!out_file.is_open()){
        std::cout << "Error opening" << std::endl;
        return false;
    }

    for(int i = 0; i < length; i++){
        out_file << info[i].name << ","
                << info[i].ID << ","
                << info[i].math << ","
                << info[i].english << ","
                << info[i].cplusplus << ","
                << info[i].total << std::endl;
    }
    return true;
}

void StartMenu(){
    std::cout << "=============================Student Infomation Maneger======================" <<std::endl;
    std::cout << "Please enter the student infomation below (q or Q  quit)." <<std::endl;
}

void End(){
    std::cout << "==================================== END ====================================" <<std::endl;
    std::cout << "program end. thanks for using." <<std::endl;
}

void CheckNum(){
    char p = std::cin.peek();
    if(p < '0' || p > '9'){
        std::cin.ignore(20);
    }
    std::cout << "enter error, try again please." << std::endl;
}
